class Solution {
public:
    int timeRequiredToBuy(vector<int>& tickets, int k) {
        int ret=0;
        for(int i=0;i<k;i++)
        {
            
            if(tickets[i]==0)
            {
                continue;
            }
            ret+=min(tickets[i],tickets[k]);
        }
        for(int i=k+1;i<tickets.size();i++)
        {
            if(tickets[i]==0)
            {
                continue;
            }
            if(tickets[k]-1>0)
            {
                ret+=min(tickets[i],tickets[k]-1);
            }
        }
        ret+=tickets[k];
        return ret;
    }
};
